import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution29 {
    public int num = 0;
    public int[][] floodFill(int[][] image, int sr, int sc, int color) {
        num = image[sr][sc];
        image[sr][sc] = color;
        if(num != color) dfs(image,sr,sc,color);
        return image;
    }

    int[] dx = {1,-1,0,0};
    int[] dy = {0,0,1,-1};
    public void dfs(int[][] image,int x,int y,int color) {
        for(int k = 0; k < 4; k++) {
            int x1 = x + dx[k];
            int y1 = y + dy[k];
            if(x1 >= 0 && x1 < image.length && y1 >= 0 && y1 < image[0].length && image[x1][y1] == num) {
                image[x1][y1] = color;
                dfs(image,x1,y1,color);
            }
        }
    }
}